import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


public class Stax {
 
	static final String NAZEV = "nazev";
	static final String JMENO = "jmeno";
	static final String PRIJMENI = "prijmeni";
	static final String TEST = "test";
	/**
	 * @param args
	 */
	
	 public List<Data> readConfig(String configFile) {
		    List<Data> datav = new ArrayList<Data>();
		    try {
		      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
		      InputStream in = new FileInputStream(configFile);
		      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
		      Data data = new Data();

		      while (eventReader.hasNext()) {
		        XMLEvent event = eventReader.nextEvent();
		        
		        if (event.isStartElement()) {
		            StartElement startElement = event.asStartElement();
		           
		           
		            if (startElement.getName().getLocalPart() == (TEST)) {
		             
		              @SuppressWarnings("unchecked")
					Iterator<Attribute> attributes = startElement.getAttributes();
		              while (attributes.hasNext()) {
		                Attribute attribute = attributes.next();
		                if (attribute.getName().toString().equals(NAZEV)) {
		                  data.setNazev(attribute.getValue());
		                }

		              }
		            }


		            if (event.asStartElement().getName().getLocalPart()
		                .equals(JMENO)) {
		              event = eventReader.nextEvent();
		              data.setJmeno(event.asCharacters().getData());
		              continue;
		            }

		            if (event.asStartElement().getName().getLocalPart()
		                .equals(PRIJMENI)) {
		              event = eventReader.nextEvent();
		              data.setPrijmeni(event.asCharacters().getData());
		              continue;
		            }

		            
		          }
		          
		          if (event.isEndElement()) {
		            EndElement endElement = event.asEndElement();
		            if (endElement.getName().getLocalPart() == (TEST)) {
		              datav.add(data);
		            }
		          }

		        }
		      } catch (FileNotFoundException e) {
		        e.printStackTrace();
		      } catch (XMLStreamException e) {
		        e.printStackTrace();
		      }
		      return datav;
		    }

	


}
